package org.kordakus.shoot;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.log4j.Logger;
import org.kordakus.shoot.gui.ImportProgressDialog;
import org.kordakus.shoot.model.ImageFile;

public class ShootController
{

	private static final Logger	_logger	= Logger.getLogger(ShootController.class);

	private final ShootContext	_context;

	public ShootController()
	{
		_context = new ShootContext();
	}

	public ShootController(final ShootContext context)
	{
		this._context = context;
	}

	public ShootContext getContext()
	{
		return _context;
	}

	public void importFiles(final Collection<File> files)
	{
		if (files != null)
		{
			_logger.info("Importing " + files.size() + " files...");
			ImportProgressDialog dialog = new ImportProgressDialog(null);
			try
			{
				int progress = 0;
				Collection<ImageFile> images = new ArrayList<ImageFile>();
				for (File file : files)
				{
					dialog.setProgress((int) ((double) progress++ / (double) files.size() * 100.0));
					dialog.setStatus(file.getAbsolutePath());
					images.add(new ImageFile(file));
				}
				_context.addImages(images);
			} catch (IOException e)
			{
				_logger.error("Failed to import file");
			} finally
			{
				dialog.dispose();
			}
		}
	}
}
